package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"note.zhiwangyunchuang.com/model"
	"note.zhiwangyunchuang.com/network"
	"note.zhiwangyunchuang.com/service"
)

func Register(ctx *gin.Context) {
	var user model.User
	ctx.Bind(&user)
	fmt.Println("user=====>", user)
	if len(user.Password) < 6 {
		network.Fail(ctx, "密码长度小于6位", gin.H{})
		return
	}
	if len(user.Mobile) != 11 {
		network.Fail(ctx, "手机号格式不正确", gin.H{})
		return
	}
	res, err, token := service.UserRegister(user)
	if !res {
		network.Fail(ctx, err, gin.H{})
		return
	}
	network.Success(ctx, "注册成功", gin.H{
		"token": token,
	})
}

func Login(ctx *gin.Context) {
	var user model.User
	ctx.Bind(&user)
	fmt.Println("user=====>", user)
	if len(user.Password) < 6 {
		network.Fail(ctx, "密码长度小于6位", gin.H{})
		return
	}
	if len(user.Mobile) != 11 {
		network.Fail(ctx, "手机号格式不正确", gin.H{})
		return
	}
	res, err, token := service.UserLogin(user)
	if !res {
		network.Fail(ctx, err, gin.H{})
		return
	}
	// 获取token
	network.Success(ctx, "登录成功", gin.H{
		"token": token,
	})
}
